package org.example.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.example.springboot.DTO.BookDTO;
import org.example.springboot.common.Result;
import org.example.springboot.entity.Book;
import org.example.springboot.service.BookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书管理控制器
 */
@Tag(name="图书管理接口")
@RestController
@RequestMapping("/book")
public class BookController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BookController.class);
    
    @Resource
    private BookService bookService;

    @Operation(summary = "分页查询图书")
    @GetMapping("/list")
    public Result<?> getBooksByPage(
            @RequestParam(required = false) Long categoryId,
            @RequestParam(defaultValue = "") String keyword,
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "borrowedCount") String orderBy) {
        Page<Book> page = bookService.getBooksByPage(categoryId, keyword, status,currentPage, size, orderBy);
        return Result.success(page);
    }
    
    @Operation(summary = "根据ID获取图书详情")
    @GetMapping("/{id}")
    public Result<?> getBookById(@PathVariable Long id) {
        Book book = bookService.getBookById(id);
        return Result.success(book);
    }
    
    @Operation(summary = "添加图书")
    @PostMapping("/add")
    public Result<?> addBook(@RequestBody @Valid BookDTO bookDTO) {
        Long bookId = bookService.addBook(bookDTO);
        return Result.success("添加成功", bookId);
    }
    
    @Operation(summary = "更新图书")
    @PutMapping("/{id}")
    public Result<?> updateBook(@PathVariable Long id, @RequestBody @Valid BookDTO bookDTO) {
        bookService.updateBook(id, bookDTO);
        return Result.success("更新成功");
    }
    
    @Operation(summary = "删除图书")
    @DeleteMapping("/{id}")
    public Result<?> deleteBook(@PathVariable Long id) {
        bookService.deleteBook(id);
        return Result.success("删除成功");
    }
    
    @Operation(summary = "更新图书状态")
    @PutMapping("/{id}/status")
    public Result<?> updateBookStatus(@PathVariable Long id, @RequestParam Integer status) {
        bookService.updateBookStatus(id, status);
        return Result.success("状态更新成功");
    }
    
    @Operation(summary = "获取热门图书")
    @GetMapping("/hot")
    public Result<?> getHotBooks(@RequestParam(defaultValue = "10") Integer limit) {
        List<Book> books = bookService.getHotBooks(limit);
        return Result.success(books);
    }
    
    @Operation(summary = "获取最新图书")
    @GetMapping("/new")
    public Result<?> getNewBooks(@RequestParam(defaultValue = "10") Integer limit) {
        List<Book> books = bookService.getNewBooks(limit);
        return Result.success(books);
    }
    
    @Operation(summary = "获取热门收藏图书")
    @GetMapping("/hot-collection")
    public Result<?> getHotCollectionBooks(@RequestParam(defaultValue = "10") Integer limit) {
        List<Book> books = bookService.getHotCollectionBooks(limit);
        return Result.success(books);
    }
    
    @Operation(summary = "获取评分最高图书")
    @GetMapping("/top-rated")
    public Result<?> getTopRatedBooks(@RequestParam(defaultValue = "10") Integer limit) {
        List<Book> books = bookService.getTopRatedBooks(limit);
        return Result.success(books);
    }
} 